package com.lgh.ibs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lgh.ibs.constants.myenum.IbsMavenRootTypeEnum;
import com.lgh.ibs.constants.myenum.PomVersionLevelEnum;
import com.lgh.ibs.constants.myenum.SnapshotOptionCodeEnum;
import com.lgh.ibs.entity.IbsRootMaven;
import com.lgh.ibs.entity.MavenPom;
import com.lgh.ibs.utils.MavenPomUtil;

public class ProjectVersionManager {
	
	/**
	 * 根据参数获取 修改的版本号信息
	 * @param isVersionModify 是否修改本工程版本号
	 * @param isParentVersionModify 是否修改本工程对应 的parent版本号
	 * @param isMax 是否是大需求
	 * @param isMin 是否是小需求
	 * @param isBug 是否是缺陷修复
	 * @param projectVersion 本工程当前版本号
	 * @param projectParentVersion 本工程当前的Parent版本号
	 * @return
	 * @throws Exception 
	 */
	public MavenPom getModifyVersionNumByParam(boolean isVersionModify, boolean isParentVersionModify,
			boolean isMax, boolean isMin, boolean isBug, 
			String projectVersion, String projectParentVersion, Integer optionType) throws Exception {
		if(isVersionModify && isMax){//当前版本 + 大需求 
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.MaxVersion.getCode(), optionType);
		}
		if(isVersionModify && isMin){//当前版本 + 小需求
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.MinVersion.getCode(), optionType);
		}
		if(isVersionModify && isBug){//当前版本 + 缺陷修复 
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.bugVersion.getCode(), optionType);
		}
		if(isParentVersionModify && isMax){//parent版本 + 大需求 
			projectParentVersion = MavenPomUtil.modifyPomVersion(projectParentVersion, 
					PomVersionLevelEnum.MaxVersion.getCode(), optionType);
		}
		if(isParentVersionModify && isMin){//parent版本 + 小需求 
			projectParentVersion = MavenPomUtil.modifyPomVersion(projectParentVersion, 
					PomVersionLevelEnum.MinVersion.getCode(), optionType);
		}
		if(isParentVersionModify && isBug){//parent版本 + 缺陷修复
			projectParentVersion = MavenPomUtil.modifyPomVersion(projectParentVersion, 
					PomVersionLevelEnum.bugVersion.getCode(), optionType);
		}
		MavenPom maven = new MavenPom();
		maven.setProjectVersion(projectVersion);
		maven.setParentVersion(projectParentVersion);
		return maven;
	}

	/**
	 * 
	 * @param projectVersion
	 * @param projectParentVersion
	 * @param optionCode
	 * @return
	 */
	public MavenPom snapshotAddOrDelOption(String projectVersion, String projectParentVersion,
			boolean isCurrVersionFlag, boolean isParentVersionFalg, Integer optionCode) {
		if(isCurrVersionFlag){//需要 操作当前版本
			if(projectVersion != null && projectVersion.trim() !=""){
				projectVersion = snapshotOption(projectVersion, optionCode);
			}
		}
		
		if(isParentVersionFalg){
			if(projectParentVersion != null && projectParentVersion.trim() !=""){
				projectParentVersion = snapshotOption(projectParentVersion, optionCode);
			}
		}
		MavenPom maven = new MavenPom();
		maven.setProjectVersion(projectVersion);
		maven.setParentVersion(projectParentVersion);
		return maven;
	}
	
	/**
	 * @param version 版本号
	 * @param optionCode 操作码
	 * @return
	 */
	private String snapshotOption(String version, Integer optionCode){
		if(SnapshotOptionCodeEnum.Add.getCode().equals(optionCode)){
			if(!version.contains("-SNAPSHOT")){
				version = version + "-SNAPSHOT";
			}
		}else if(SnapshotOptionCodeEnum.Del.getCode().equals(optionCode)){
			if(version.contains("-SNAPSHOT")){
				version = version.replace("-SNAPSHOT", "");
			}
		}
		return version;
	}
	
	/**
	 * @Title: saveSelectionData 
	 * @Description: 保存数据到文件
	 * @param mavenPom
	 * @throws Exception 
	 * @throws 
	 * @author guohao.li
	 * @dateTime 2016年12月28日 上午11:21:38
	 */
	public void saveSelectionData(MavenPom mavenPom) throws Exception {
		String pomFilePath = mavenPom.getPomFilePath();
		String projectVersion = mavenPom.getProjectVersion();
		String parentVersion = mavenPom.getParentVersion();
		if(projectVersion != null && projectVersion != ""){
			String projectExpress = "/project/version";
			String parentExpress = "/project/parent/version";
			
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put(projectExpress, projectVersion);
			
			if(parentVersion != null && parentVersion != ""){
				dataMap.put(parentExpress, parentVersion);
			}
			MavenPomUtil.modifyPomVersionByFile(pomFilePath, dataMap);
			System.out.println("dfsdfas");
		}
	}
	
	/**
	 * 获取root工程的版本信息
	 * @param pomFilePath
	 * @return
	 * @throws Exception 
	 */
	public List<IbsRootMaven> getRootProjectChildrenVersionList(String pomFilePath) throws Exception {
		List<IbsRootMaven> list = MavenPomUtil.getRootProjectChildrenVersionList(pomFilePath);
		return list;
	}

	/**
	 * 根据参数获取 修改的版本号信息
	 * @param isMax 是否是大需求
	 * @param isMin 是否是小需求
	 * @param isBug 是否是缺陷修复
	 * @param projectVersion 本工程当前版本号
	 * @param projectParentVersion 本工程当前的Parent版本号
	 * @return
	 * @throws Exception 
	 */
	public IbsRootMaven getRootProjectModifyVersionNumByParam(boolean isMax, boolean isMin, boolean isBug, 
			String projectVersion, Integer optionType) throws Exception {
		if(isMax){//当前版本 + 大需求 
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.MaxVersion.getCode(), optionType);
		}
		if(isMin){//当前版本 + 小需求
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.MinVersion.getCode(), optionType);
		}
		if(isBug){//当前版本 + 缺陷修复 
			projectVersion = MavenPomUtil.modifyPomVersion(projectVersion, 
					PomVersionLevelEnum.bugVersion.getCode(), optionType);
		}
		
		IbsRootMaven maven = new IbsRootMaven();
		maven.setProjectVersion(projectVersion);
		return maven;
	}

	public IbsRootMaven rootProjectSnapshotAddOrDelOption(String projectVersion, Integer optionCode) {
		if(projectVersion != null && projectVersion.trim() !=""){
			projectVersion = snapshotOption(projectVersion, optionCode);
		}
		IbsRootMaven maven = new IbsRootMaven();
		maven.setProjectVersion(projectVersion);
		return maven;
	}
	
	/**
	 * @Title: saveRootPomSelectionData 
	 * @Description:保存rootpom的版本信息 
	 * @param maven 
	 * @param rootPomFilePath root pom文件路径
	 * @throws Exception 
	 * @throws 
	 * @author guohao.li
	 * @dateTime 2016年12月29日 上午9:19:20
	 */
	public void saveRootPomSelectionData(List<IbsRootMaven> list, String rootPomFilePath) throws Exception {
		if(list != null && list.size() > 0){
			Map<String, String> dataMap = new HashMap<String, String>();
			for (IbsRootMaven maven : list) {
				if(maven.getProjectType().equals(IbsMavenRootTypeEnum.Root.getCode())){
					String parentExpress = "/project/version";
					dataMap.put(parentExpress, maven.getProjectVersion());
//					System.out.println("========root==============="+ parentExpress + "  " + maven.getProjectName());
					
				}else if(maven.getProjectType().equals(IbsMavenRootTypeEnum.Children.getCode())){
					String parentExpress = "/project/properties/" + maven.getProjectName();
					dataMap.put(parentExpress, maven.getProjectVersion());
//					System.out.println("========Children==============="+ parentExpress + "  " + maven.getProjectName());
				}
			}
			MavenPomUtil.modifyPomVersionByFile(rootPomFilePath, dataMap);
			
		}
	}
}
